package com.yxk.bootvueshop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.xml.internal.bind.v2.TODO;
import com.yxk.bootvueshop.common.StatusCode;
import com.yxk.bootvueshop.config.ServiceException;
import com.yxk.bootvueshop.entity.SpManager;
import com.yxk.bootvueshop.entity.dto.SpManagerDTO;
import com.yxk.bootvueshop.entity.vo.ManagerVO;
import com.yxk.bootvueshop.mapper.SpManagerMapper;
import com.yxk.bootvueshop.service.SpManagerService;
import com.yxk.bootvueshop.tool.BeanMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;

@Service
@Slf4j
public class SpManagerServiceImpl implements SpManagerService {

    @Autowired
    private SpManagerMapper spManagerMapper;

    @Autowired
    private BeanMapper beanMapper;


    @Override
    public Page<ManagerVO> getMangerByLike(Page<ManagerVO> page, String query) {
        return (Page<ManagerVO>) spManagerMapper.selectPageVo(page, query);
    }

    @Override
    public ManagerVO create(SpManagerDTO spManagerDTO) {
        QueryWrapper<SpManager> spManagerQueryWrapper = new QueryWrapper<>();
        //查询用户名是否存在
        spManagerQueryWrapper.eq("mg_name", spManagerDTO.getUsername());
        SpManager hasSpManger = spManagerMapper.selectOne(spManagerQueryWrapper);
        if (hasSpManger != null) {
            //用户名存在
            throw new ServiceException(StatusCode.BADREQUEST, "用户名已存在");
        }
        SpManager spManager = new SpManager();
        spManager.setMgName(spManagerDTO.getUsername());
        spManager.setMgEmail(spManagerDTO.getEmail());
        spManager.setMgMobile(spManagerDTO.getMobile());
        //TODO 引入了认证模块之后这里再进行加密
        spManager.setMgPwd(spManagerDTO.getPassword());
        spManager.setMgState((byte) 1); //默认激活
        spManager.setRoleId((byte) -1); //默认角色
        spManager.setMgTime(Calendar.getInstance().getTimeInMillis());

        int i = spManagerMapper.insert(spManager);
        log.info("SpManager插入结果：" + i);
        System.out.println("SpManager插入结果：" + i);
        if (i == 1) {
            //添加成功，查询该对象
            //根据用户名来查询
            SpManager spManger = spManagerMapper.selectOne(spManagerQueryWrapper);
            ManagerVO map = new ManagerVO(spManger.getMgId(), spManager.getMgName(), spManager.getMgMobile(), spManager.getMgEmail(), spManager.getRoleId(), spManager.getMgTime());
            return map;
        } else {
            throw new ServiceException(StatusCode.INTERNAL_SERVER_ERROR, "插入失败");
        }
    }

    /**
     * 修改用户状态
     *
     * @param uId  用户 ID
     * @param type 用户状态 值为 true 或者 false
     * @return
     */
    @Override
    public ManagerVO updateState(Integer uId, String type) {
        SpManager spManager = new SpManager();
        spManager.setMgId(uId);
        spManager.setMgState((byte) (type.equals("true") ? 1 : 0));

        int update = spManagerMapper.updateById(spManager);
        if (update != 1) {
            throw new ServiceException(StatusCode.BADREQUEST, "设置状态失败");
        }
        SpManager spManager1 = spManagerMapper.selectById(uId);
        return new ManagerVO(spManager1.getMgId(), spManager1.getRoleId(), spManager1.getMgName(), spManager1.getMgMobile(),
                spManager1.getMgEmail(), spManager1.getMgState());
    }

    /**
     * 根据 ID 查询用户信息根据 ID 查询用户信息
     *
     * @param id 用户 ID
     * @return
     */
    @Override
    public ManagerVO getById(Integer id) {
        SpManager spManager = spManagerMapper.selectById(id);
        if (ObjectUtil.isEmpty(spManager)) {
            throw new ServiceException(StatusCode.BADREQUEST, "查询失败");
        } else {
            return new ManagerVO(spManager.getMgId(), spManager.getMgName(), spManager.getRoleId(), spManager.getMgMobile(), spManager.getMgEmail());
        }

    }

    /**
     * 编辑用户提交
     *
     * @param id
     * @param dto
     * @return
     */
    @Override
    public ManagerVO updateManger(Integer id, SpManagerDTO dto) {
        //DTO -> POJO
        SpManager spManager = new SpManager(dto.getEmail(),dto.getMobile());
        spManager.setMgId(id);
        int update = spManagerMapper.updateById(spManager);
        if( update != 1){
            throw new ServiceException(StatusCode.INTERNAL_SERVER_ERROR,"更新失败");
        }
        SpManager spManager1 = spManagerMapper.selectById(id);
        return new ManagerVO(spManager1.getMgId(), spManager1.getMgName(), spManager1.getRoleId(), spManager1.getMgMobile(), spManager1.getMgEmail());
    }

    /**
     * 删除单个用户
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        int i = spManagerMapper.deleteById(id);
        return i;
    }

    @Override
    public ManagerVO updateRole(Integer id, SpManagerDTO dto) {
        SpManager spManager = new SpManager();
        spManager.setMgId(id);
        spManager.setRoleId((byte)dto.getRid().intValue());
        int i = spManagerMapper.updateById(spManager);
        if(i != 1){
            throw new ServiceException(StatusCode.INTERNAL_SERVER_ERROR,"设置角色失败");
        }
        SpManager spManager1 = spManagerMapper.selectById(id);
        return new ManagerVO(spManager1.getMgId(), spManager1.getMgName(), spManager1.getRoleId(), spManager1.getMgMobile(), spManager1.getMgEmail());
    }
}
